#include <board.h>
#include <board_memories.h>
#include <lcd/lcdd.h>
#include <lcd/draw.h>
#include <memories/MEDSdcard.h>
#include <ff.h>

#define SCREEN_MENU     1
#define SCREEN_VIEWER   2
static volatile int screen = SCREEN_MENU;

static unsigned char *pLcdBuffer = (unsigned char *) (AT91C_EBI_SDRAM
        + 0x01000000);

#define _CHECK_FR(res, msg) \
    if (res != FR_OK) { \
        LCDD_DrawString(pLcdBuffer, 10, 10, msg, COLOR_BLACK); \
        return 0; \
    }

#define _F1_    "Cenas verdes a voar."
#define _F1_SZ  15 + 5
#define _F2_    "Cenas verdes a voando."
#define _F2_SZ  15 + 7
#define _F3_    "Cenas verdes a moverem-se no ar."
#define _F3_SZ  15 + 17
#define _F4_    "Cenas verdes a pairar."
#define _F4_SZ  15 + 7

DWORD get_fattime( void )
{
    unsigned int date;

    date = ((2008 - 1980) << 25)
        | (01 << 21)
        | (14 << 16)
        | (WORD) (17 << 11)
        | (WORD) (16 << 5)
        | (WORD) (11 >> 1);

    return date;
}

Media sdcard;

int main () {
    FATFS fs = {0};
    FIL file;
    FRESULT res;
    unsigned int dwr;

    // Initialize LCD
    LCDD_Fill(pLcdBuffer, COLOR_WHITE);
    LCDD_Initialize();
    LCDD_DisplayBuffer(pLcdBuffer);

#if !defined(sdram)
    BOARD_ConfigureSdram(BOARD_SDRAM_BUSWIDTH);
#endif

    MEDSdcard_Initialize(&sdcard, 1);

    // mount
    res = f_mount(DRV_MMC, &fs);
    _CHECK_FR(res, "NOT MOUNTED!");

    // format
    res = f_mkfs(DRV_MMC, 0, SECTOR_SIZE_SDCARD);
    _CHECK_FR(res, "NOT FORMATTED!");

    // make some files and folders
    { // 1
        res = f_open(&file, "0:file1.txt", FA_CREATE_ALWAYS | FA_WRITE);
        _CHECK_FR(res, "F1: NOT OPENED!");
        res = f_write(&file, _F1_, _F1_SZ, &dwr);
        _CHECK_FR(res, "F1: NOT WRITTEN!");
        res = f_close(&file);
        _CHECK_FR(res, "F1: NOT CLOSED!");
    }
    { // 2
        res = f_open(&file, "0:file2.txt", FA_CREATE_ALWAYS | FA_WRITE);
        _CHECK_FR(res, "F2: NOT OPENED!");
        res = f_write(&file, _F2_, _F2_SZ, &dwr);
        _CHECK_FR(res, "F2: NOT WRITTEN!");
        res = f_close(&file);
        _CHECK_FR(res, "F2: NOT CLOSED!");
    }
    { // a
        res = f_mkdir("0:folderA");
        _CHECK_FR(res, "FA: NOT MADE!");
        { // 3
            res = f_open(&file, "0:folderA/file3.txt", FA_CREATE_ALWAYS | FA_WRITE);
            _CHECK_FR(res, "F3: NOT OPENED!");
            res = f_write(&file, _F3_, _F3_SZ, &dwr);
            _CHECK_FR(res, "F3: NOT WRITTEN!");
            res = f_close(&file);
            _CHECK_FR(res, "F3: NOT CLOSED!");
        }
        { // b
            res = f_mkdir("0:folderA/folderB");
            _CHECK_FR(res, "FB: NOT MADE!");
        }
    }
    { // 4
        res = f_open(&file, "0:file4.txt", FA_CREATE_ALWAYS | FA_WRITE);
        _CHECK_FR(res, "F4: NOT OPENED!");
        res = f_write(&file, _F4_, _F4_SZ, &dwr);
        _CHECK_FR(res, "F4: NOT WRITTEN!");
        res = f_close(&file);
        _CHECK_FR(res, "F4: NOT CLOSED!");
    }
    { // c
        res = f_mkdir("0:folderC");
        _CHECK_FR(res, "FC: NOT MADE!");
    }

    // everything is all right
    LCDD_DrawString(pLcdBuffer,
            10, 10,
            "Uptight, out of sight",
            COLOR_BLACK);

    return 0;
}
